package model

import business.Attackable
import business.AutoMoveable
import business.Destoryable
import business.Sufferable
import enums.Direction
import org.itheima.kotlin.game.core.Painter

/**
 * 子弹
 *
 * 自动移动的能力
 * 销毁的能力
 * 攻击的能力
 * 接受攻击的能力
 * */
class Bullet(var gradle:Int,var ower:IView,override val currentDirection: Direction, create: (Int, Int) -> Pair<Int,Int>)
    :AutoMoveable,Destoryable,Attackable,Sufferable{

    override var badDirection: Direction?=null;
    override var speed: Int=8

    override var pool: Int=1

    override val power: Int=1
    override var isAttack: Boolean=false

    override var x: Int=100
    override var y: Int=100
    override var width: Int=GameConfig.BLOCK_WIDTH
    override var height: Int=GameConfig.BLOCK_WIDTH

    var imgPath=when(currentDirection){
        Direction.UP->"img/bullet_u.gif"
        Direction.DOWN->"img/bullet_d.gif"
        Direction.LEFT->"img/bullet_l.gif"
        Direction.RIGHT->"img/bullet_r.gif"
    }

    init {
        /** 获取图片宽高*/
        var size=Painter.size(imgPath)
        width=size[0]
        height=size[1]

        /** 获取坐标值 x y  */
        var pair=create.invoke(width,height)
        x=pair.first
        y=pair.second

        if(gradle==1){
            speed=8
        }else if(gradle==2){
            speed=12
        }
    }


    override fun draw() {
        Painter.drawImage(imgPath,x,y)
    }

    /** 自动移动 */
    override fun move(direction: Direction) {
        when(currentDirection){
            Direction.UP->y-=speed
            Direction.DOWN->y+=speed
            Direction.LEFT->x-=speed
            Direction.RIGHT->x+=speed
        }
    }

    /** 是否已经超出游戏窗口 */
    override fun isDestory(): Boolean {

        /** 如果子弹已经产生了攻击，子弹应该销毁*/
        if(isAttack) return true

        /** 如果子弹已经被攻击了，子弹应该销毁*/
        if(pool<=0) return  true

        return when(currentDirection){
            Direction.UP->y<-height
            Direction.DOWN->y>GameConfig.WINDOW_HEIGHT
            Direction.LEFT->x<-width
            Direction.RIGHT->x>GameConfig.WINDOW_WIDTH
        }
    }

    /**  通知产生了攻击*/
    override fun notifyAttack(sufferable: Sufferable) {
        isAttack=true
    }

    override fun notifySuffer(attack: Attackable): ArrayList<IView> {
        pool-=attack.power
        return ArrayList<IView>()
    }

}